<?php
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$_W['page']['title'] = '自动推送黑名单管理';

if(!pdo_fieldexists('tg_times', 'activity_before')) {
    pdo_query("ALTER TABLE ".tablename('tg_times')." ADD `activity_before` int(11) NOT NULL COMMENT '活动前自动推送时间';");
}
if(!pdo_fieldexists('tg_times', 'activity_after')) {
    pdo_query("ALTER TABLE ".tablename('tg_times')." ADD `activity_after` int(11) NOT NULL COMMENT '活动后自动推送时间';");
}
if(!pdo_fieldexists('tg_times', 'status')) {
    pdo_query("ALTER TABLE ".tablename('tg_times')." ADD `status` int(11) NOT NULL COMMENT '是否开启自动推送';");
}

if ($operation == 'display') {

    $size = 10;
    $page = !empty($_GPC['page'])?$_GPC['page']:1;
    $sql = "select * from ".tablename('tg_blacklist')." where  uniacid=:uniacid  LIMIT " . ($page - 1) * $size . " , " . $size;
    $list = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid']));

    $total = pdo_fetchall("select id from ".tablename('tg_blacklist')." WHERE uniacid = {$_W['uniacid']}");
    $total = count($total);

    $pager = pagination($total, $page, $size);

//    $list = pdo_fetchall("SELECT * FROM " . tablename('tg_blacklist'));
    foreach ($list as &$value) {
        $member = pdo_fetch("SELECT * FROM " .tablename('tg_member') ." where openid=:openid", array(":openid" => $value['openid']));
        $value['nickname'] = $member['nickname'];
    }

    include wl_template('member/blacklist');
} elseif ($operation == 'post') {

//    $id = $_GPC['id'];
    $openid = $_GPC['openid'];
    $list = pdo_fetch("SELECT * FROM " . tablename('tg_blacklist') ." where openid = :openid" , array(':openid' => $openid));

    if (!empty($list)) {
//        pdo_update('tg_blacklist', $data, array('id' => $list['id']));
        message('该用户已在黑名单中!', web_url('member/blacklist', array('op' => '')), 'success');
    } else {
        $member = pdo_fetch("select * from " .tablename('tg_member') ." where from_user = '" .$openid ."'");
//        die(json_encode(array('member' => $member)));
        $data = array(
            'uniacid' => $_W['uniacid'],
            'openid' => $openid,
            'idcard' => $member['idcard']
        );
        pdo_insert('tg_blacklist', $data);
        $id = pdo_insertid();
        message('添加黑名单成功！', web_url('member/blacklist', array('op' => '')), 'success');
    }
    include wl_template('member/blacklist');

} elseif ($operation == 'delete') {

    $uniacid = $_W['uniacid'];
    $funcop = $_GPC['funcop'];
    if (empty($funcop)){
        $id = intval($_GPC['id']);
        $blacklist = pdo_fetch("SELECT * FROM " . tablename('tg_blacklist') . " WHERE id = '$id'");
        if (empty($blacklist)) {
            message('抱歉，黑名单不存在或是已经被删除！', web_url('member/blacklist', array('op' => 'display')), 'error');
        }
        $idcard = $blacklist['idcard'];
        $member = pdo_fetchall("select from_user from " .tablename('tg_member') ." where idcard = '" .$idcard ."' ");
        foreach ($member as &$value) {
            pdo_update('tg_order' , array('black' => -1) , array('uniacid' => $uniacid , 'openid' => $value['from_user'] , 'black' => 1));
        }
        pdo_delete('tg_blacklist', array('id' => $id));
        message('黑名单删除成功！', web_url('member/blacklist', array('op' => 'display')), 'success');
    } elseif($funcop == 'deleteAll'){
        $openid = pdo_fetchall("select openid from " .tablename('tg_blacklist'));
        foreach ($openid as &$value) {
            pdo_update('tg_order' , array('black' => -1) , array('uniacid' => $uniacid , 'openid' => $value['openid'] , 'black' => 1));
        }
        pdo_delete('tg_blacklist');
        message('清空黑名单成功！', web_url('member/blacklist', array('op' => 'display')), 'success');
    }
//    elseif($funcop == 'deleteCheck'){
//        $ids = $_GPC['ids'];
//        $blacklist = pdo_fetch("SELECT * FROM " . tablename('tg_blacklist') . " WHERE id = '$id'");
//        if (empty($blacklist)) {
//            message('抱歉，黑名单不存在或是已经被删除！', web_url('member/blacklist', array('op' => 'display')), 'error');
//        }
//        $openid = $blacklist['openid'];
//        pdo_update('tg_order' , array('status' => 9) , array('uniacid' => $uniacid , 'openid' => $openid , 'attend' => 0));
//        pdo_delete('tg_blacklist', array('id' => $ids));
//        message('批量删除黑名单成功！', web_url('member/blacklist', array('op' => 'display')), 'success');
//    }
    include wl_template('member/blacklist');

} elseif ($operation == 'set') {

    $uniacid = $_W['uniacid'];

    if (checksubmit('submit')) {
        $id = $_GPC['id'];
        $data = array(
            'uniacid' => $uniacid,
            'blacktime' => $_GPC['blacktime'],
            'activity' => $_GPC['activity'],
            'activity_before' => $_GPC['activity_before'],
            'activity_after' => $_GPC['activity_after'],
            'status' => $_GPC['status']
        );
        if (!empty($id)) {
            pdo_update('tg_times', $data, array('id' => $id));
            message('更新成功！', web_url('member/blacklist', array('op' => 'set')), 'success');
        } else {
            pdo_insert('tg_times', $data);
            $id = pdo_insertid();
            message('添加成功！', web_url('member/blacklist', array('op' => 'set')), 'success');
        }
    }
    $list = pdo_fetch("SELECT * FROM " . tablename('tg_times') ." where uniacid = :uniacid" , array(':uniacid' => $uniacid));
    include wl_template('member/blacklist');
} else {
    message('请求方式不存在');
}
exit();